#include <sstream>
#include "../Common/PokerTool.h"
#include "GameInfo.h"

int GameInfo::getwinner()
{
	return winner;
}
/*
 * determine le joueur gagnant en fonction de la dernière action du joueur
 */
int GameInfo::getwinner(map <int,Player> &players)
{
	if(players[1].getAction().toStdString() == choice[0])
	{
		return 2;
	}
	else
	{
		return 1;
	}
}

GameInfo::GameInfo()
{
	pot=0;
	gameState = 0;
	bigBlind = BIGBLIND;
	winner=0;
	//setCurrentPlayer(1);
	// Initilalise les cartes à -1
	int i=0;

	for(i = 0; i < 5 ;i++)
		board[i].setNumero(-1);
}

GameInfo::GameInfo(const GameInfo & gameInfo)
{
 pot 	   = gameInfo.pot;
 gameState = gameInfo.gameState;
 players   = gameInfo.players;
}

GameInfo & GameInfo::operator = (const GameInfo & gameInfo)
{
 pot 	   = gameInfo.pot;
 gameState = gameInfo.gameState;
 players   = gameInfo.players;
}

/**
 * Incrémente Game State
 */
void GameInfo::incrementGameState()
{
	cout << "Etat en cours de la partie : " << gameState << endl;
	if(gameState <3) gameState++;
}

/**
 * Getter
 */
QMap<int,Card> GameInfo::getBoardCards()
{
	return board;
}

map <int,Player> &GameInfo::getPlayers()
{
 return players;
}

Player &GameInfo::getPlayer(int numPlayer)
{
	return players[numPlayer];
}

int GameInfo::getPot()
{
	return pot;
}

int GameInfo::getGameState()
{
 return gameState;
}

int GameInfo::getBigBlind()
{
	return bigBlind;
}

int GameInfo::getCurrentPlayer()
{
	return currentPlayer;
}

int GameInfo::getOtherCurrentPlayer()
{
	if(currentPlayer ==1)
		return 2;
	else
		return 1;
}

int GameInfo::getAmountRaised()
{
	return amountRaised;
}

QString GameInfo::getAmountRaisedQString()
{
	return QString::number(amountRaised);
}

/**
 * Setter
 */
void GameInfo::setPlayers(map <int,Player> _players)
{
	players = _players;
}
void GameInfo::setBoardCards(Card cardFlop1, Card cardFlop2, Card cardFlop3, Card cardTurn, Card cardRiver)
{
	board[0] = cardFlop1;
	board[1] = cardFlop2;
	board[2] = cardFlop3;
	board[3] = cardTurn;
	board[4] = cardRiver;
}

void GameInfo::setPlayer(int numPlayer, Player player)
{
 players[numPlayer] = player;
}

void GameInfo::setPot(int _pot)
{
	pot = _pot;
}

void GameInfo::setGameState(int _gameState)
{
	gameState = _gameState;
}

void GameInfo::setBigBlind(int _bigBlind)
{
	bigBlind = _bigBlind;
}

void GameInfo::setCurrentPlayer(int _currentPlayer)
{
	cout << "setCurrentPlayer : " <<  _currentPlayer << endl;
	currentPlayer = _currentPlayer;
}

void GameInfo::setAmountRaised(int _amountRaised)
{
	amountRaised = _amountRaised;
}

void GameInfo::setBoardCards(int k,Card c)
{
	board[k] = c;
}

void GameInfo::setWinner(int _winner)
{
 winner = _winner;
}

QString GameInfo::toString()
{
	QString ret="";
	for(int i=0;i<5;i++)
	{
		ret += ("carte : " + board[i].toString() +'\n');
	}

	ret += QString( ("gameState : " +PokerTool::intToString(gameState)+'\n').c_str() ) ;

	ret += QString( ("bigBlind : " + PokerTool::intToString(bigBlind)+'\n').c_str() ) ;

	ret += QString( ("amountRaised : " + PokerTool::intToString(amountRaised)+'\n').c_str() ) ;

	ret.append("[flop");

	if( gameState >= 1)
	{
		ret.append("["+getBoardCards()[0].toString()+"]" );
		ret.append("["+getBoardCards()[1].toString()+"]" );
		ret.append("["+getBoardCards()[2].toString()+"]" );
	}
	if( gameState >= 2)
	{
		ret.append("["+getBoardCards()[3].toString()+"]" );
	}
	if( gameState >= 3)
	{
		ret.append("["+getBoardCards()[4].toString()+"]" );
	}
	ret.append("]");
	return ret;
}

QString GameInfo::toXml()
{
 QDomDocument doc;

 QDomNode pokerGameNode = doc.createElement("pokerGame");

 QDomElement stateElement = doc.createElement("state");
 QDomText stateText = doc.createTextNode(QString::number(gameState));
 stateElement.appendChild(stateText);

 QDomElement bigBlindElement = doc.createElement("bigBlind");
 QDomText bigBlindText = doc.createTextNode(QString::number(bigBlind));
 bigBlindElement.appendChild(bigBlindText);

 QDomElement currentPlayerElement = doc.createElement("currentPlayer");
 QDomText currentPlayerText = doc.createTextNode(QString::number(currentPlayer));
 currentPlayerElement.appendChild(currentPlayerText);

 QDomElement cardElement;

 QDomElement flopElement = doc.createElement("flop");
 cardElement = getBoardCards()[0].toXml(doc);
 flopElement.appendChild(cardElement);
 cardElement = getBoardCards()[1].toXml(doc);
 flopElement.appendChild(cardElement);
 cardElement = getBoardCards()[2].toXml(doc);
 flopElement.appendChild(cardElement);

 QDomElement turnElement = doc.createElement("turn");
 cardElement = getBoardCards()[3].toXml(doc);
 turnElement.appendChild(cardElement);

 QDomElement riverElement = doc.createElement("river");
 cardElement = getBoardCards()[4].toXml(doc);
 riverElement.appendChild(cardElement);

 pokerGameNode.appendChild(stateElement);
 pokerGameNode.appendChild(bigBlindElement);
 pokerGameNode.appendChild(currentPlayerElement);
 pokerGameNode.appendChild(flopElement);
 pokerGameNode.appendChild(turnElement);
 pokerGameNode.appendChild(riverElement);

 QDomElement playerElement;

 // Ugly part : it's not dynamic
 playerElement = getPlayer(0).toXml(doc);
 pokerGameNode.appendChild(playerElement);

 playerElement = getPlayer(1).toXml(doc);
 pokerGameNode.appendChild(playerElement);

 doc.appendChild(pokerGameNode);

 return doc.toString();
}
